<?php
// 自定义命令行任务
declare(strict_types=1);

namespace app\command;


use app\admin\jobs\TimeoutCancelOrder;
use app\admin\jobs\CheckMissingLogisticsRule;
use app\admin\jobs\CheckMissingDiscountPrice;
use app\admin\service\Order as OrderService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;


class task extends Command
{


    /**
     * @describe 配置参数 需要其他参数 自行添加
     * @author: Bobo
     * @date: 2025-07-18 13:16:50 
     * @param [type] @required
     * @return Renderable
     */
    protected function configure()
    {
        // 指令配置
        $this->setName('task')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->setDescription('the task command');
    }

    /**
     * @describe 执行自定义命令行任务
     * @author: Bobo
     * @date: 2025-07-18 13:16:50 
     * @param [type] @required
     * @return Renderable
     */
    protected function execute(Input $input, Output $output)
    {
        $name = $input->getArgument('name');
        try {
            $output->writeln("开始执行任务: {$name}");

            switch ($name) {
                case 'timeoutCancelOrder': // 超时取消订单 
                    $handle = new TimeoutCancelOrder();
                    $handle->execute();
                    $output->writeln("timeoutCancelOrder 执行完成");
                    break;
                case 'checkMissingLogisticsRule': // 检查物流规则缺失数据 
                    $handle = new CheckMissingLogisticsRule();
                    $handle->execute();
                    $output->writeln("checkMissingLogisticsRule 执行完成");
                    break;
                case 'checkMissingDiscountPrice': // 检查折扣价格缺失数据
                    $handle = new CheckMissingDiscountPrice();
                    $handle->execute();
                    $output->writeln("checkMissingDiscountPrice 执行完成");
                    break;
                case 'exportOrder': // 导出订单数据
                    $handle = new OrderService();
                    $result = $handle->export();
                    $output->writeln("exportOrder 执行完成");
                    break;
                default:
                    $output->writeln('参数错误，可用参数: timeoutCancelOrder, checkMissingLogisticsRule, checkMissingDiscountPrice, exportOrder');
            }
        } catch (\Throwable $e) {
            $output->writeln("任务执行失败: " . $e->getMessage());
            $output->writeln("文件: " . $e->getFile() . " 行: " . $e->getLine());
            $output->writeln("堆栈跟踪: " . $e->getTraceAsString());
        }
    }
}
